package br.com.xp.galera.login.bean;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import br.com.xp.galera.domain.Usuario;
import br.com.xp.galera.exception.UsuarioInvalidoException;
import br.com.xp.galera.login.service.LoginService;

public class LoginMBean {
	
	private LoginService loginService;
	private String login;
	private String senha;
	
	public String entrar() {

		try {
			Usuario usuario = loginService.entrar(getLogin(), getSenha());
			
			HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			session.setAttribute("idUsuario", usuario.getId());
			
		} catch (UsuarioInvalidoException uie) {
            FacesMessage fm = new FacesMessage(uie.getMessage());
            FacesContext.getCurrentInstance().addMessage(uie.getMessage(), fm);
            return "usuario-invalido";
		}
		
		return "entrar";
		
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getSenha() {
		return senha;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}

	public LoginService getLoginService() {
		return loginService;
	}

	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
	
}
